﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WoodAttachCtrl : MonoBehaviour
{
    public float angleCanAttachEdge = 30.0f;

    private bool hasAttachPlayer = false;

    void OnCollisionStay2D(Collision2D coll)
    {
        if (coll.gameObject.tag == "Player")
        {
            //Debug.Log("WoodAttachCtrl -> OnCollisionStay2D");

            if (!hasAttachPlayer)
            {

                float angle = Vector2.Angle(Vector2.up, coll.transform.position - transform.position);
                //Debug.Log("angle:" + angle);
				if (angle < angleCanAttachEdge && !coll.gameObject.GetComponent<PlayerCtrl>().isDie)
                {
                    coll.transform.SetParent(transform);
                    coll.transform.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
                    hasAttachPlayer = true;
                }
				if (coll.gameObject.GetComponent<PlayerCtrl> ().isDie) {
					GetComponent<CircleCollider2D> ().isTrigger = true;
				}
            }
        }
    }

    void OnCollisionExit2D(Collision2D coll)
    {
        coll.transform.SetParent(null);
        hasAttachPlayer = false;
    }
}